package com.sochinchou.res.views;

import android.content.Context;
import android.graphics.*;
import android.view.*;
import android.view.View.MeasureSpec;
import android.util.AttributeSet;
import android.content.res.Resources;

public class MyCompassView extends View {

	private Paint markerPaint;
	private Paint markerPaint2;
	private Paint textPaint;
	private Paint circlePaint;
	

	private float bearing = 0;

	public void setBearing(float _bearing) {
		bearing = _bearing;
	}

	public float getBearing() {
		return bearing;
	}

	public MyCompassView(Context context) {
		super(context);
		initCompassView();
	}

	public MyCompassView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initCompassView();
	}

	public MyCompassView(Context context, AttributeSet ats, int defaultStyle) {
		super(context, ats, defaultStyle);
		initCompassView();
	}

	protected void initCompassView() {
		setFocusable(true);

		circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		circlePaint.setColor(0xff0000ff);
		circlePaint.setStyle(Paint.Style.FILL_AND_STROKE);
		circlePaint.setStrokeWidth(1);

		textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		textPaint.setColor(0xffff0000);
		textPaint.setTextSize(20);


		markerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		markerPaint.setColor(0xff00ff00);
		
		markerPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
		markerPaint2.setColor(0xffffff00);
		

	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int measuredWidth = measure(widthMeasureSpec);
		int measuredHeight = measure(heightMeasureSpec);
		int d = Math.min(measuredWidth, measuredHeight);
		setMeasuredDimension(d, d);
	}

	private int measure(int measureSpec) {
		int result = 0;
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);

		if (specMode == MeasureSpec.UNSPECIFIED) {
			result = 200;
		} else {
			result = specSize;
		}
		return result;
	}

	
	@Override
	protected void onDraw(Canvas canvas) {
		int px = getMeasuredWidth() /2 ;
		int py = getMeasuredHeight() / 2;
		int radius = Math.min(px, py);
		canvas.drawCircle(px, py, radius, circlePaint);
		
		canvas.save();
		canvas.rotate(30, px, py);

		int tWidth = (int) textPaint.measureText("W");
		int tx = px - tWidth / 2;
		int ty = py - radius + 20;
		
		for( int i = 0; i < 12; i ++){
			canvas.drawLine(px, 0, px, 20, markerPaint);

			canvas.save();
			canvas.translate(0, 20);
			
			canvas.drawText(""+(i+1), tx, ty, textPaint);
		
			canvas.restore();
			canvas.rotate(30, px, py);
		}
	
		canvas.restore();
		
		canvas.save();
		canvas.rotate(bearing, px, py);
		canvas.drawCircle(px, py, 5, markerPaint);
		canvas.drawLine(px, py, px, py-100, textPaint);
		canvas.restore();
	
	}
	
	

}